@ECHO ON

::ENABLE STATUS MAILS SENDING : 0 - OFF ; 1 - ON
SET enable_mail=1

:: Initialize dates for full archive
SET full_archive_date=(1 2 4 23)

:: Initialize archive time for full archive
SET full_archive_time=(22 0 11 14 19 21 20) 


:: Initialize dates for incremental archive
SET inc_archive_date=(1 2 4 22 23 24 20)

:: Initialize archive time for incremental archive
SET inc_archive_time=(19 21 0 10 11 13 16) 


:: Define directories to be archived
SET archive_dir_list=("C:\Projects\NikiStill\archive_me1" "C:\Projects\NikiStill\archive_me2")

::Define how long to keep the local file archives
SET days_to_keep=1

:: DEFINE VARIABLES AND REGISTER PATH ENVIRONMENT VARIABLES HERE

:: Define FOREFILE path
SET forfiles=%CD%%~n1\fileop\forfiles

:: Define send_mail path
SET send_mail=%CD%%~n1\send_mail

:: Define ftp path
SET ftp_file=%CD%%~n1\ftp_file

:: Define RAR path
PATH "C:\Program Files (x86)\WinRAR"

:: Define backup directory path
SET back_up_path=C:\Projects\NikiStill\backup

:: Initialize current day
SET today=%date:~7,2%

:: Initialize current hour
SET hour=%time:~0,2%

:: Initialize full archive name
set full_archive_name=%date:~10,4%_%date:~4,2%_%date:~7,2%
for /f %%a in (full_rar_name.txt) do set full_archive_name=%%a
if %full_archive_name%==NULL full_archive_name=%date:~10,4%_%date:~4,2%_%date:~7,2%

:: Initialize incremental archive name
set time_sufix=%time:~0,2%_%time:~3,2%
SET inc_archive_name=%full_archive_name%___%date:~10,4%_%date:~7,2%_%date:~4,2%_%time_sufix: =%

:: DEFINE FUNCTIONS HERE

::Function - Full arechive
:full_check_day
for %%i in %full_archive_date% do if %today%==%%i goto full_check_hour
echo "Full archive is not scheduled for that day!" 
goto inc_check_day

:full_check_hour
for %%k in %full_archive_time% do if %hour: =%==%%k goto full_archive
echo "Full archive is not scheduled for that hour!"
goto inc_check_day

:full_archive
for %%j in %archive_dir_list% do rar a -m5mdgrtrr -x@excludes.txt -ep1 -ac %back_up_path%\%full_archive_name%%~n1.rar %%j

echo %date:~10,4%_%date:~4,2%_%date:~7,2% > full_rar_name.txt

if %enable_mail%==1 ( if %ERRORLEVEL%==0 ( call send_mail "Full Archive %date:~10,4%/%date:~4,2%/%date:~7,2% %time:~0,2%:%time:~3,2%%~n1-ARCHIVED_SUCCESSFULL" "Full archive was created successfully with name: %full_archive_name%%~n1.rar" ) else ( call send_mail "Full Archive %date:~10,4%/%date:~4,2%/%date:~7,2% %time:~0,2%:%time:~3,2%%~n1-ARCHIVE_FAIL" "Fail to create the full archive on %date:~10,4%/%date:~4,2%/%date:~7,2% at %time:~0,2%:%time:~3,2% %~n1ERRORCODE:%ERRORLEVEL%" ) )

call ftp_file %back_up_path% %full_archive_name%%~n1.rar

if %enable_mail%==1 ( if %ERRORLEVEL%==0 ( call send_mail "Upload Full Archive %date:~10,4%/%date:~4,2%/%date:~7,2% %time:~0,2%:%time:~3,2%%~n1-UPLOADED_SUCCESSFULL" "Full archive was uploaded successfully with name: %full_archive_name%%~n1.rar" ) else ( call send_mail "Upload Full Archive %date:~10,4%/%date:~4,2%/%date:~7,2% %time:~0,2%:%time:~3,2%%~n1-UPLOAD_FAIL" "Fail to upload the full archive on %date:~10,4%/%date:~4,2%/%date:~7,2% at %time:~0,2%:%time:~3,2% %~n1ERRORCODE:%ERRORLEVEL%" ) )

::Function - Incremental arechive
:inc_check_day
for %%i in %inc_archive_date% do if %today%==%%i goto inc_check_hour
echo "Incremental archive is not scheduled for that day!"
goto delete_old_files

:inc_check_hour
for %%k in %inc_archive_time% do if %hour%==%%k goto inc_archive
echo "Incremental archive is not scheduled for that hour!"
goto delete_old_files

:inc_archive
for %%j in %archive_dir_list% do rar a -m5mdgrtrr -x@excludes.txt -ep1 -ac -ao %back_up_path%\%inc_archive_name%%~n1.rar %%j

if %enable_mail%==1 ( if %ERRORLEVEL%==0 ( call send_mail "Incremental Archive %date:~10,4%/%date:~4,2%/%date:~7,2% %time:~0,2%:%time:~3,2%%~n1-ARCHIVE_SUCCESSFULL" "Incremental archive was created successfully with name: %inc_archive_name%%~n1.rar" ) else ( if %ERRORLEVEL%==10 ( call send_mail "Incremental Archive %date:~10,4%/%date:~4,2%/%date:~7,2% %time:~0,2%:%time:~3,2%%~n1-NOT_NEEDED" "There are no updates to be archived on %date:~10,4%/%date:~4,2%/%date:~7,2% at %time:~0,2%:%time:~3,2%!" ) else ( call send_mail "Incremental Archive %date:~10,4%/%date:~4,2%/%date:~7,2% %time:~0,2%:%time:~3,2%%~n1-ARCHIVE_FAIL" "Fail to create the incremental archive on %date:~10,4%/%date:~4,2%/%date:~7,2% at %time:~0,2%:%time:~3,2% %~n1ERRORCODE:%ERRORLEVEL%" ) ) )

call ftp_file "%back_up_path%" %inc_archive_name%%~n1.rar

if %enable_mail%==1 ( if %ERRORLEVEL%==0 ( call send_mail "Upload Incremental Archive %date:~10,4%/%date:~4,2%/%date:~7,2% %time:~0,2%:%time:~3,2%%~n1-UPLOAD_SUCCESSFULL" "Incremental archive was uploaded successfully with name: %inc_archive_name%%~n1.rar" ) else ( if %ERRORLEVEL%==1 ( call send_mail "Upload Incremental Archive %date:~10,4%/%date:~4,2%/%date:~7,2% %time:~0,2%:%time:~3,2%%~n1-NOT_NEEDED" "There are no archive updates to be uploaded on %date:~10,4%/%date:~4,2%/%date:~7,2% at %time:~0,2%:%time:~3,2%!" ) else ( call send_mail "Upload Incremental Archive %date:~10,4%/%date:~4,2%/%date:~7,2% %time:~0,2%:%time:~3,2%%~n1-UPLOAD_FAIL" "Fail to upload the incremental archive on %date:~10,4%/%date:~4,2%/%date:~7,2% at %time:~0,2%:%time:~3,2% %~n1ERRORCODE:%ERRORLEVEL%" ) ) )

:delete_old_files
call %forfiles% -p%back_up_path% -m*.rar -c"cmd /c del "@FILE"" -d-%days_to_keep%

:end






